﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections.Generic;

public partial class Controls_BrandSeries : System.Web.UI.UserControl
{
	public enum EShow
	{	
		All,
		Brand,
		Series
	}
	public EShow ShowWhat { get; set; }

	public enum EItem
	{
		DoNotAdd,
		ItemPleaseChoose,
		ItemAll
	}
	public EItem BrandAddListItem { get; set; }
	public EItem SeriesAddListItem { get; set; }

	private BrandManager m_BrandManager				= new BrandManager();
	private ProductSeriesManager m_SeriesManger		= new ProductSeriesManager();

	public Brand SelectedBrand
	{
		get
		{
			if (this.ddlBrand.Enabled == true)
			{
				return this.m_BrandManager.GetBrandById(new Guid(this.ddlBrand.SelectedValue));
			}
			return null;
		}
		set
		{
			this.BindBrand();
			this.ddlBrand.SelectedIndex		= this.ddlBrand.Items.IndexOf(this.ddlBrand.Items.FindByValue(value.BrandId.ToString()));
		}
	}

	public ProductSeries SelectedSeries
	{
		get
		{
			if (this.ddlSeries.Enabled == true && !this.ddlSeries.SelectedValue.Equals("*") &&! this.ddlSeries.SelectedValue.Equals("."))
			{
				return this.m_SeriesManger.GetProductSeriesById(new Guid(this.ddlSeries.SelectedValue));
			}
			return null;
		}
		set
		{
			this.BindBrand();
			this.ddlBrand.SelectedIndex		= this.ddlBrand.Items.IndexOf(this.ddlBrand.Items.FindByValue(value.BrandId.ToString()));
			this.BindSeries();
			this.ddlSeries.SelectedIndex	= this.ddlSeries.Items.IndexOf(this.ddlSeries.Items.FindByValue(value.ProductSeriesId.ToString()));
		}
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		switch (this.ShowWhat)
		{
			case EShow.All:
				this.ddlBrand.AutoPostBack	= true;
				break;
			case EShow.Series:
				this.ddlBrand.Visible		= false;
				this.ddlBrand.AutoPostBack	= true;
				break;
			case EShow.Brand:
			default:
				this.ddlBrand.AutoPostBack	= false;
				this.ddlSeries.Visible		= false;
				break;
		}

		if (!IsPostBack)
		{
			this.BindBrand();
		}
	}

	private void BindBrand()
	{
		this.ddlBrand.Items.Clear();
		List<Brand> theBrandList		= this.m_BrandManager.GetAll();


		if (theBrandList.Count > 0)
		{
			this.ddlBrand.DataSource		= theBrandList;
			this.ddlBrand.DataTextField		= "Name";
			this.ddlBrand.DataValueField	= "BrandId";
			this.ddlBrand.DataBind();
			this.ddlBrand.Enabled			= true;
			this.BrandAddItem();
			if (this.ShowWhat != EShow.Brand)
			{
				this.BindSeries();
			}
		}
		else
		{
			this.AddNoDataItemToDropDownList(ddlBrand);
		}
	}

	private void BindSeries()
	{
		this.ddlSeries.Items.Clear();

		if (this.ddlBrand.SelectedValue.Equals("*") || this.ddlBrand.SelectedValue.Equals("."))
		{
			this.ddlSeries.Visible	= false;
			return;
		}

		List<ProductSeries> theSeriesList	= this.m_SeriesManger.GetProductSeriesByBrand(new Guid(this.ddlBrand.SelectedValue));

		if (theSeriesList.Count > 0)
		{
			this.ddlSeries.DataSource		= theSeriesList;
			this.ddlSeries.DataTextField	= "Name";
			this.ddlSeries.DataValueField	= "ProductSeriesId";
			this.ddlSeries.DataBind();
			this.ddlSeries.Enabled			= true;
			this.ddlSeries.Visible			= true;
			this.SeriesAddItem();
		}
		else
		{
			this.AddNoDataItemToDropDownList(ddlSeries);
		}
	}

	protected void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
	{
		this.BindSeries();
	}

	private void AddNoDataItemToDropDownList(DropDownList aDropDownList)
	{
		aDropDownList.Items.Add(new ListItem("No Data", string.Empty));
		aDropDownList.SelectedIndex		= 0;
		aDropDownList.Enabled			= false;
	}



	private void BrandAddItem()
	{
		switch (this.BrandAddListItem)
		{
			case EItem.ItemPleaseChoose:
				this.ddlBrand.Items.Insert(0, new ListItem("请选择...", "."));
				break;
			case EItem.ItemAll:
				this.ddlBrand.Items.Insert(0, new ListItem("全部", "*"));
				break;
			case EItem.DoNotAdd:
			default:
				break;
		}
	}
	private void SeriesAddItem()
	{
		switch (this.SeriesAddListItem)
		{
			case EItem.ItemPleaseChoose:
				this.ddlSeries.Items.Insert(0, new ListItem("请选择...", "."));
				break;
			case EItem.ItemAll:
				this.ddlSeries.Items.Insert(0, new ListItem("全部", "*"));
				break;
			case EItem.DoNotAdd:
			default:
				break;
		}
	}
}
